package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;

import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/20/13
 * Time: 1:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class Dropdown extends Control {

    java.util.List<JTextField> options;
    java.util.List<String> optionsText;
    private JComboBox optionsList;

    public Dropdown() {
        fieldType = ControlBeanConstant.DROPDOWNLIST;
        options = new ArrayList<JTextField>();
        optionsText = new ArrayList<String>();
        optionsList = new JComboBox();

        options.add(new Option(true));

        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        if ( controlPanel != null && controlPanel.getComponentCount() > 0 ) {
            controlPanel.removeAll();
        } else {
            controlPanel = new JPanel(new GridBagLayout());
        }

        label = new JTextField(super.labelText);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.ipadx = 130;
        constraints.insets = new Insets(5, 5, 0, 5);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.fill = GridBagConstraints.BOTH;

        controlPanel.add(label, constraints);

        //TODO when user input grows extremely long, the original limit of the row is not enough.
        GridBagConstraints comboBoxConstraints = new GridBagConstraints();
        comboBoxConstraints.gridx = 1;
        comboBoxConstraints.gridy = 0;
        //comboBoxConstraints.ipadx = 100;
        comboBoxConstraints.insets = new Insets(5, 0, 0, 5);
        comboBoxConstraints.anchor = GridBagConstraints.NORTH;
        comboBoxConstraints.fill = GridBagConstraints.HORIZONTAL;


        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.insets = new Insets(5, 0, 0, 5);
        constraints.anchor = GridBagConstraints.NORTH;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        optionsText.clear();
        int row = 1;
        for( JTextField current : options ) {
            constraints.gridy = row++;
            controlPanel.add(current, constraints);
            if (!current.getText().equals("") && !current.getText().equalsIgnoreCase("edit option") )
                optionsText.add(current.getText());
        }

        optionsList.setModel(new DefaultComboBoxModel(optionsText.toArray()));
        controlPanel.add(optionsList, comboBoxConstraints);

        controlPanel.addFocusListener(new ControlFocusAdaptor());
        controlPanel.addMouseListener(new ControlMouseAdaptor());

        label.addFocusListener(new ControlFocusAdaptor());
        label.addMouseListener(new ControlMouseAdaptor());

        return controlPanel;
    }

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel    = new JLabel("Control Name");
        controlName     = new JTextField();

        hint            = new JCheckBox("Hint");
        required        = new JCheckBox("Required");
        readOnly        = new JCheckBox("Read Only");

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
//        constraints.ipadx = 130;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 6;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }


    //TODO fix the buggy display
    void addOption() {
        options.add(new Option(true));
        System.out.println("An option has been added need to rebuild list.");
        buildControlPanel();
        controlPanel.revalidate();
        //TODO update size
    }


    class Option extends JTextField implements ActionListener, FocusListener {

        boolean addOption = false;
        public Option() {
            super("Edit Option");
//            addActionListener(this);
        }

        public Option(boolean add) {
            super("Edit Option");
            addOption = true;
            addActionListener(this);
        }
        public Option(String name) {
            super(name);
//            addActionListener(this);
        }

        @Override
        public void focusLost(FocusEvent e) {
            if (addOption  && !getText().equalsIgnoreCase("Edit Option")) {
                addOption();
                addOption = false;
                //TODO update size
            }
        }

        @Override
        public void focusGained(FocusEvent e) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (addOption  && !getText().equalsIgnoreCase("Edit Option")) {
                addOption();
                addOption = false;
                //TODO update size
            }
        }
    }

    private String concat() {
        String concatOptions = new String();

        for ( JTextField currentCheck : options ) {
            concatOptions += currentCheck.getText()+",";
        }

        //trim the last comma away and return
        return concatOptions.substring(0, concatOptions.length()-1);
    }

    public String[] breakdown(String options) {
        return options.split(",");
    }

    public void buildOptionsFromString(String[] input) {
        if ( options == null ) {
            options = new ArrayList<JTextField>();
        } else {
            options.clear();
        }

        for ( String current : input ) {
            options.add(new Option(current));
        }

        options.add(new Option(true));
        buildControlPanel();
    }

    public ControlPropertiesBean exportData() {
        super.exportData();
        try {
            controlPropertiesBean.addProperty(ControlBeanConstant.OPTIONS, concat());
        } catch ( Exception e ) {
            System.out.println("Drop down list not set.");
        }
        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);
            buildOptionsFromString(breakdown(controlPropertiesBean.getProperty(ControlBeanConstant.OPTIONS)));
        } catch ( Exception e ) {
            System.out.println("Loading failed at drop down");
        }
    }
}
